//------------------------------
// The fastMavlink library
// (c) OlliW, OlliW42, www.olliw.eu
//------------------------------

#pragma once
#ifndef FASTMAVLINK_MSG_PL_FDB_INFO_H
#define FASTMAVLINK_MSG_PL_FDB_INFO_H
#include "PwrLink.h"


//----------------------------------------
//-- Message PL_FDB_INFO
//----------------------------------------

// fields are ordered, as they appear on the wire
FASTMAVLINK_PACK(
typedef struct _fmav_pl_fdb_info_t {
    uint16_t bat_v;
    int16_t bat_c;
    uint8_t mode_code;
    uint8_t error_code;
    uint8_t power_key_state[2];
    uint8_t debug_key_state;
}) fmav_pl_fdb_info_t;


#define FASTMAVLINK_MSG_ID_PL_FDB_INFO  0

#define FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX  9
#define FASTMAVLINK_MSG_PL_FDB_INFO_CRCEXTRA  113

#define FASTMAVLINK_MSG_PL_FDB_INFO_FLAGS  0
#define FASTMAVLINK_MSG_PL_FDB_INFO_TARGET_SYSTEM_OFS  0
#define FASTMAVLINK_MSG_PL_FDB_INFO_TARGET_COMPONENT_OFS  0

#define FASTMAVLINK_MSG_PL_FDB_INFO_FRAME_LEN_MAX  34

#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_POWER_KEY_STATE_NUM  2 // number of elements in array
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_POWER_KEY_STATE_LEN  2 // length of array = number of bytes

#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_BAT_V_OFS  0
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_BAT_C_OFS  2
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_MODE_CODE_OFS  4
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_ERROR_CODE_OFS  5
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_POWER_KEY_STATE_OFS  6
#define FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_DEBUG_KEY_STATE_OFS  8


//----------------------------------------
//-- Message PL_FDB_INFO pack,encode routines, for sending
//----------------------------------------

FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_pack(
    fmav_message_t* _msg,
    uint8_t sysid,
    uint8_t compid,
    uint16_t bat_v, int16_t bat_c, uint8_t mode_code, uint8_t error_code, const uint8_t* power_key_state, uint8_t debug_key_state,
    fmav_status_t* _status)
{
    fmav_pl_fdb_info_t* _payload = (fmav_pl_fdb_info_t*)_msg->payload;

    _payload->bat_v = bat_v;
    _payload->bat_c = bat_c;
    _payload->mode_code = mode_code;
    _payload->error_code = error_code;
    _payload->debug_key_state = debug_key_state;
    memcpy(&(_payload->power_key_state), power_key_state, sizeof(uint8_t)*2);

    _msg->sysid = sysid;
    _msg->compid = compid;
    _msg->msgid = FASTMAVLINK_MSG_ID_PL_FDB_INFO;
    _msg->target_sysid = 0;
    _msg->target_compid = 0;
    _msg->crc_extra = FASTMAVLINK_MSG_PL_FDB_INFO_CRCEXTRA;
    _msg->payload_max_len = FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX;

    return fmav_finalize_msg(_msg, _status);
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_encode(
    fmav_message_t* _msg,
    uint8_t sysid,
    uint8_t compid,
    const fmav_pl_fdb_info_t* _payload,
    fmav_status_t* _status)
{
    return fmav_msg_pl_fdb_info_pack(
        _msg, sysid, compid,
        _payload->bat_v, _payload->bat_c, _payload->mode_code, _payload->error_code, _payload->power_key_state, _payload->debug_key_state,
        _status);
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_pack_to_frame_buf(
    uint8_t* _buf,
    uint8_t sysid,
    uint8_t compid,
    uint16_t bat_v, int16_t bat_c, uint8_t mode_code, uint8_t error_code, const uint8_t* power_key_state, uint8_t debug_key_state,
    fmav_status_t* _status)
{
    fmav_pl_fdb_info_t* _payload = (fmav_pl_fdb_info_t*)(&_buf[FASTMAVLINK_HEADER_V2_LEN]);

    _payload->bat_v = bat_v;
    _payload->bat_c = bat_c;
    _payload->mode_code = mode_code;
    _payload->error_code = error_code;
    _payload->debug_key_state = debug_key_state;
    memcpy(&(_payload->power_key_state), power_key_state, sizeof(uint8_t)*2);

    _buf[5] = sysid;
    _buf[6] = compid;
    _buf[7] = (uint8_t)FASTMAVLINK_MSG_ID_PL_FDB_INFO;
    _buf[8] = ((uint32_t)FASTMAVLINK_MSG_ID_PL_FDB_INFO >> 8);
    _buf[9] = ((uint32_t)FASTMAVLINK_MSG_ID_PL_FDB_INFO >> 16);

    return fmav_finalize_frame_buf(
        _buf,
        FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX,
        FASTMAVLINK_MSG_PL_FDB_INFO_CRCEXTRA,
        _status);
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_encode_to_frame_buf(
    uint8_t* _buf,
    uint8_t sysid,
    uint8_t compid,
    const fmav_pl_fdb_info_t* _payload,
    fmav_status_t* _status)
{
    return fmav_msg_pl_fdb_info_pack_to_frame_buf(
        _buf, sysid, compid,
        _payload->bat_v, _payload->bat_c, _payload->mode_code, _payload->error_code, _payload->power_key_state, _payload->debug_key_state,
        _status);
}


#ifdef FASTMAVLINK_SERIAL_WRITE_CHAR

FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_pack_to_serial(
    uint8_t sysid,
    uint8_t compid,
    uint16_t bat_v, int16_t bat_c, uint8_t mode_code, uint8_t error_code, const uint8_t* power_key_state, uint8_t debug_key_state,
    fmav_status_t* _status)
{
    fmav_pl_fdb_info_t _payload;

    _payload.bat_v = bat_v;
    _payload.bat_c = bat_c;
    _payload.mode_code = mode_code;
    _payload.error_code = error_code;
    _payload.debug_key_state = debug_key_state;
    memcpy(&(_payload.power_key_state), power_key_state, sizeof(uint8_t)*2);

    return fmav_finalize_serial(
        sysid,
        compid,
        (uint8_t*)&_payload,
        FASTMAVLINK_MSG_ID_PL_FDB_INFO,
        FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX,
        FASTMAVLINK_MSG_PL_FDB_INFO_CRCEXTRA,
        _status);
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_encode_to_serial(
    uint8_t sysid,
    uint8_t compid,
    const fmav_pl_fdb_info_t* _payload,
    fmav_status_t* _status)
{
    return fmav_finalize_serial(
        sysid,
        compid,
        (uint8_t*)_payload,
        FASTMAVLINK_MSG_ID_PL_FDB_INFO,
        FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX,
        FASTMAVLINK_MSG_PL_FDB_INFO_CRCEXTRA,
        _status);
}
#endif


//----------------------------------------
//-- Message PL_FDB_INFO decode routines, for receiving
//----------------------------------------
// For these functions to work correctly, the msg payload must be zero-filled.
// Call the helper fmav_msg_zerofill() if needed, or set FASTMAVLINK_ALWAYS_ZEROFILL to 1
// Note that the parse functions do zero-fill the msg payload, but that message generator functions
// do not. This means that for the msg obtained from parsing the below functions can safely be used,
// but that this is not so for the msg obtained from pack/encode functions.

FASTMAVLINK_FUNCTION_DECORATOR void fmav_msg_pl_fdb_info_decode(fmav_pl_fdb_info_t* payload, const fmav_message_t* msg)
{
#if FASTMAVLINK_ALWAYS_ZEROFILL
    if (msg->len < FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX) {
        memcpy(payload, msg->payload, msg->len);
        // ensure that returned payload is zero-filled
        memset(&(((uint8_t*)payload)[msg->len]), 0, FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX - msg->len);
    } else {
        // note: msg->len can be larger than PAYLOAD_LEN_MAX if the message has unknown extensions
        memcpy(payload, msg->payload, FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX);
    }
#else
    // this requires that msg payload had been zero-filled before
    memcpy(payload, msg->payload, FASTMAVLINK_MSG_PL_FDB_INFO_PAYLOAD_LEN_MAX);
#endif
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t fmav_msg_pl_fdb_info_get_field_bat_v(const fmav_message_t* msg)
{
    uint16_t r;
    memcpy(&r, &(msg->payload[0]), sizeof(uint16_t));
    return r;
}


FASTMAVLINK_FUNCTION_DECORATOR int16_t fmav_msg_pl_fdb_info_get_field_bat_c(const fmav_message_t* msg)
{
    int16_t r;
    memcpy(&r, &(msg->payload[2]), sizeof(int16_t));
    return r;
}


FASTMAVLINK_FUNCTION_DECORATOR uint8_t fmav_msg_pl_fdb_info_get_field_mode_code(const fmav_message_t* msg)
{
    uint8_t r;
    memcpy(&r, &(msg->payload[4]), sizeof(uint8_t));
    return r;
}


FASTMAVLINK_FUNCTION_DECORATOR uint8_t fmav_msg_pl_fdb_info_get_field_error_code(const fmav_message_t* msg)
{
    uint8_t r;
    memcpy(&r, &(msg->payload[5]), sizeof(uint8_t));
    return r;
}


FASTMAVLINK_FUNCTION_DECORATOR uint8_t fmav_msg_pl_fdb_info_get_field_debug_key_state(const fmav_message_t* msg)
{
    uint8_t r;
    memcpy(&r, &(msg->payload[8]), sizeof(uint8_t));
    return r;
}


FASTMAVLINK_FUNCTION_DECORATOR uint8_t* fmav_msg_pl_fdb_info_get_field_power_key_state_ptr(const fmav_message_t* msg)
{
    return (uint8_t*)&(msg->payload[6]);
}


FASTMAVLINK_FUNCTION_DECORATOR uint8_t fmav_msg_pl_fdb_info_get_field_power_key_state(uint16_t index, const fmav_message_t* msg)
{
    if (index >= FASTMAVLINK_MSG_PL_FDB_INFO_FIELD_POWER_KEY_STATE_NUM) return 0;
    return ((uint8_t*)&(msg->payload[6]))[index];
}


//----------------------------------------
//-- Pymavlink wrappers
//----------------------------------------
#ifdef FASTMAVLINK_PYMAVLINK_ENABLED

#define MAVLINK_MSG_ID_PL_FDB_INFO  0

#define mavlink_pl_fdb_info_t  fmav_pl_fdb_info_t

#define MAVLINK_MSG_ID_PL_FDB_INFO_LEN  9
#define MAVLINK_MSG_ID_PL_FDB_INFO_MIN_LEN  9
#define MAVLINK_MSG_ID_0_LEN  9
#define MAVLINK_MSG_ID_0_MIN_LEN  9

#define MAVLINK_MSG_ID_PL_FDB_INFO_CRC  113
#define MAVLINK_MSG_ID_0_CRC  113

#define MAVLINK_MSG_PL_FDB_INFO_FIELD_POWER_KEY_STATE_LEN 2


#if MAVLINK_COMM_NUM_BUFFERS > 0

FASTMAVLINK_FUNCTION_DECORATOR uint16_t mavlink_msg_pl_fdb_info_pack(
    uint8_t sysid,
    uint8_t compid,
    mavlink_message_t* _msg,
    uint16_t bat_v, int16_t bat_c, uint8_t mode_code, uint8_t error_code, const uint8_t* power_key_state, uint8_t debug_key_state)
{
    fmav_status_t* _status = mavlink_get_channel_status(MAVLINK_COMM_0);
    return fmav_msg_pl_fdb_info_pack(
        _msg, sysid, compid,
        bat_v, bat_c, mode_code, error_code, power_key_state, debug_key_state,
        _status);
}


FASTMAVLINK_FUNCTION_DECORATOR uint16_t mavlink_msg_pl_fdb_info_encode(
    uint8_t sysid,
    uint8_t compid,
    mavlink_message_t* _msg,
    const mavlink_pl_fdb_info_t* _payload)
{
    return mavlink_msg_pl_fdb_info_pack(
        sysid,
        compid,
        _msg,
        _payload->bat_v, _payload->bat_c, _payload->mode_code, _payload->error_code, _payload->power_key_state, _payload->debug_key_state);
}

#endif


FASTMAVLINK_FUNCTION_DECORATOR uint16_t mavlink_msg_pl_fdb_info_pack_txbuf(
    char* _buf,
    fmav_status_t* _status,
    uint8_t sysid,
    uint8_t compid,
    uint16_t bat_v, int16_t bat_c, uint8_t mode_code, uint8_t error_code, const uint8_t* power_key_state, uint8_t debug_key_state)
{
    return fmav_msg_pl_fdb_info_pack_to_frame_buf(
        (uint8_t*)_buf,
        sysid,
        compid,
        bat_v, bat_c, mode_code, error_code, power_key_state, debug_key_state,
        _status);
}


FASTMAVLINK_FUNCTION_DECORATOR void mavlink_msg_pl_fdb_info_decode(const mavlink_message_t* msg, mavlink_pl_fdb_info_t* payload)
{
    fmav_msg_pl_fdb_info_decode(payload, msg);
}

#endif // FASTMAVLINK_PYMAVLINK_ENABLED


#endif // FASTMAVLINK_MSG_PL_FDB_INFO_H
